import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:async';

class RxDartDemo extends StatelessWidget {
  const RxDartDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RxDartDemo'),
      ),
      body: RxDartHomeDemo(),
    );
  }
}

class RxDartHomeDemo extends StatefulWidget {
  RxDartHomeDemo({Key key}) : super(key: key);

  @override
  _RxDartHomeDemoState createState() => _RxDartHomeDemoState();
}

class _RxDartHomeDemoState extends State<RxDartHomeDemo> {
  PublishSubject<String> _textfieldSubject;

  @override
  void dispose() {
    super.dispose();
    _textfieldSubject.close();
  }

  @override
  void initState() {
    super.initState();

    _textfieldSubject = PublishSubject<String>();
    _textfieldSubject
    // .map((item) => '自定义前缀: $item')
    .where((item) => item.length>5).
    debounceTime(Duration(milliseconds:1000))
    .listen((data)=>print('just output $data'));

    // Stream<String> _observable = Stream.fromIterable(['hello', '您好']);
    // _observable.listen(print);
    // PublishSubject<String> _subject = PublishSubject<String>();
    // BehaviorSubject<String> _subject = BehaviorSubject<String>();
    // ReplaySubject<String> _subject = ReplaySubject<String>(maxSize: 3);
    // _subject.add('hold');
    // _subject.add('hello');
    // _subject.add('hi');
    // _subject.add('bye');
    // _subject.listen((value) {
    //   print('listen 1: $value');
    // });
    // _subject.listen((value) {
    //   print('listen 2: ${value.toUpperCase()}');
    // });

  }

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context).copyWith(
        primaryColor: Colors.black
      ), 
      child: TextField(
        onChanged: (value){
          _textfieldSubject.add('input: $value');
        },
        onSubmitted: (value){
          _textfieldSubject.add('submit: $value');
        },
        decoration: InputDecoration(
          labelText: 'Title',
          filled: true
        )
      )
    );
  }
}
